<template>
  <div id="dialogAddEdit">
    <el-dialog
      class="about-set"
      title="用户标签列表"
      :visible.sync="dialogData.isShow"
      width="515px"
      :before-close="labelHandleClose"
    >
      <div class="form-box">
        <el-form
          ref="ruleForm"
          :model="ruleForm"
          :rules="rules"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="所属标签分类" prop="tagCategoryId">
            <el-select
              v-model="ruleForm.tagCategoryId"
              filterable
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="item in tagCategoryIdList"
                :key="item.id"
                :label="item.tagCategoryName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="创建标签" prop="type">
            <el-select v-model="ruleForm.type" placeholder="请选择">
              <el-option
                v-for="item in typeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <template v-if="ruleForm.type === '0'">
            <el-form-item label="标签名" prop="tagName">
              <el-input
                v-model="ruleForm.tagName"
                maxlength="20"
                show-word-limit
                clearable
              ></el-input>
            </el-form-item>
          </template>
          <template v-else>
            <importFile
              :file-data="fileData"
              :dialog-data="importDialogData"
            ></importFile>
          </template>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" plain @click="cancelLabel">取 消</el-button>
        <el-button
          type="success"
          :loading="loading"
          @click="confirmLabel('ruleForm')"
          >确认</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { tagCategoryNames } from '@/api/lableManage/tagCategory' // 接口
import { importUserLabel } from '@/api/lableManage/labelUser' // 接口
import { downTagUrl } from '@/api/lableManage/tagManage' // 接口
import importFile from '@component/lableManage/common/importFile' // 导入
export default {
  name: 'DialogAddEdit',
  msg: '用户标签管理-新增弹窗',
  components: {
    importFile
  },
  props: ['dialogData'],
  data() {
    return {
      loading: false,
      userId: '',
      fileData: {
        labelName: '请选择', // label名称
        fileName: '', // 上传文件名称
        upFile: '' // 上传文件
      },
      // 传递给导入弹框的数据
      importDialogData: {
        downUrl: '' // 下载模标签板接口
      },
      tagCategoryIdList: [], // 标签分类列表
      typeList: [
        // 创建标签列表
        { label: '新建标签', value: '0' },
        { label: '批量导入', value: '1' }
      ],
      ruleForm: {
        tagCategoryId: '', // 标签分类
        type: '0', // 创建标签
        tagName: '' // 标签名称
      },
      rules: {
        tagName: [
          { required: true, message: '标签名不能为空', trigger: 'blur' }
          /* {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'} */
        ],
        tagCategoryId: [
          { required: true, message: '请选择标签分类', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getTagCategoryIdList()
    this.userId = this.dialogData.userId
    this.importDialogData.downUrl = downTagUrl()
  },
  methods: {
    /*获取标签分类列表*/
    getTagCategoryIdList() {
      tagCategoryNames(
        (response) => {
          this.tagCategoryIdList = response
        },
        (response) => {
          this.$message.warning(response.message)
        }
      )
    },
    labelHandleClose(done) {
      this.cancelLabel()
      done()
    },
    cancelLabel() {
      // 关闭前清空
      this.loading = false
      this.dialogData.isShow = false
      this.dialogData.userId = ''
    },
    /* 确认提交 */
    confirmLabel(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.ruleForm.type === '0') {
            //手动添加
            this.subHand()
          } else {
            //批量导入
            this.subImport()
          }
        } else {
          return false
        }
      })
    },
    /* 提交表单-手动导入 */
    subHand() {
      let { tagCategoryId, tagName } = this.ruleForm
      let obj = {
        tagName,
        tagCategoryId: tagCategoryId,
        userId: [this.userId]
      }
      importUserLabel(
        obj,
        (response) => {
          this.loading = false
          //二级页面数据增加，一级页面不需要刷新
          /* this.$emit('refresh')*/
          this.cancelLabel()
        },
        (response) => {
          this.$message.warning('新增失败,' + response.message)
          this.loading = false
        },
        `?categoryId=${tagCategoryId}`
      )
    },
    /* 提交表单-批量导入 */
    subImport() {
      if (!this.fileData.fileName) {
        this.$message({
          message: '请添加文件！',
          type: 'warning'
        })
        return
      }
      this.loading = true
      let formData = new FormData()
      formData.append('file', this.fileData.upFile)
      formData.append('tagCategoryId', this.ruleForm.tagCategoryId)
      formData.append('userId', this.userId)
      importUserLabel(
        formData,
        (response) => {
          this.loading = false
          //二级页面数据增加，一级页面不需要刷新
          /*this.$emit('refresh')*/
          this.cancelLabel()
        },
        (response) => {
          // 有上传失败的情况，弹出失败提示窗
          this.loading = false
          this.$emit('showError', response.message, 'tagManage')
          this.cancelLabel()
        },
        `?all`
      )
    },
    /* 导入失败弹窗 */
    showError(text) {
      this.errorDialogData.isShow = true // 显示导入失败弹框
      this.errorDialogData.warningText = text // 显示导入失败弹框
    }
  }
}
</script>

<style lang="less">
// #dialogAddEdit {
// }
</style>
